package br.edu.unifei.mestrado.mem.graph;

import java.util.Iterator;

import br.edu.unifei.mestrado.commons.graph.NodeWrapper;
import br.edu.unifei.mestrado.commons.graph.RelationshipWrapper;
import br.edu.unifei.mestrado.commons.partition.AbstractPartition;

public class NodeMem implements NodeWrapper {

	private Node node;

	public NodeMem(Node node) {
		this.node = node;
	}

	@Override
	public Iterable<RelationshipWrapper> getRelationships() {
		return new Iterable<RelationshipWrapper>() {

			@Override
			public Iterator<RelationshipWrapper> iterator() {
				return new Iterator<RelationshipWrapper>() {
					private Iterator<Relationship> edges = node.getRelationships().iterator();

					@Override
					public RelationshipWrapper next() {
						Relationship edge = edges.next();
						RelationshipWrapper rel = new RelationshipMem(edge);
						return rel;
					}

					@Override
					public boolean hasNext() {
						return edges.hasNext();
					}

					@Override
					public void remove() {
					}
				};
			}
		};
	}

	@Override
	public void setWeight(Integer weight) {
		node.setWeight(weight);
	}

	@Override
	public void setPartition(Integer partition) {
		if(partition == -1) { //não pode certar. ele já inicia com -1
			throw new RuntimeException("Valor de partição inválido " + partition + " para vertice: " + node.getId());
		}
		node.setPartition(partition);
	}
	
	@Override
	public void resetPartition() {
		node.setPartition(AbstractPartition.NO_PARTITION);
	}

	@Override
	public void lock() {
		node.lock();
	}

	@Override
	public void unlock() {
		node.unLock();
	}

	@Override
	public void setD(Integer d) {
		node.setD(d);
	}

	@Override
	public int getWeight() {
		return node.getWeight();
	}

	@Override
	public int getPartition() {
		return node.getPartition();
	}

	@Override
	public boolean isLocked() {
		return node.isLocked();
	}

	@Override
	public long getId() {
		return node.getId();
	}

	@Override
	public boolean hasProperty(String key) {
		return true;
	}

	public Node getInnerNode() {
		return node;
	}
	
	@Override
	public int getD() {
		return node.getD();
	}
	
	@Override
	public long getInsideOf() {
		return node.getInsideOf();
	}
	
	@Override
	public void setInsideOf(Long coarsedVertex) {
		node.setInsideOf(coarsedVertex);
	}
	
	@Override
	public boolean hasInsideOf() {
		return node.getInsideOf() != null;
	}
	
	@Override
	public boolean isCoarsed() {
		return node.isCoarsed();
	}
	
	@Override
	public void setCoarsed(boolean coarsed) {
		node.setCoarsed(coarsed);
	}
	
	@Override
	public boolean equals(Object obj) {
		if(obj instanceof NodeMem) {
			return node.equals(((NodeMem)obj).getInnerNode());
		}
		return false;
	}
	
	@Override
	public int hashCode() {
		return node.hashCode();
	}
	
	@Override
	public String toString() {
		return node.toString();
	}
}
